package com.zlp.controller;

import com.zlp.utils.CacheUpload;
import com.zlp.dto.UploadVO;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.zlp.common.utils.AliyunOSSUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.Objects;

/**
 *
 * @description: 测试OSS功能
 * @author: ZouLiPing
 * @contact: 请关注微信公众号“给自己一个smile”里面有详细电商项目文档,weixin:zlp865391093
 * @date: 2019年3月29日15:18:33
 * @version V1.0
 */
@Controller
@RequestMapping("/oss")
public class AliyunOSSController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;


    /**
     * 测试上传文件到阿里云OSS存储(不支持上传文件，推荐作为图床使用)
     *
     * @return
     */
  /*  @RequestMapping("/upload")
    @ResponseBody
    public String testUpload(HttpServletRequest request) {
        File file = new File("E:/Picture/test.jpg");
        AliyunOSSUtil aliyunOSSUtil = new AliyunOSSUtil();
        Long fileSize = (file.getSize()/1024*100)/100;
        String url = aliyunOSSUtil.upLoad(file,request);
        logger.debug("url{}",url);
        return "success";
    }*/

    /**
     * 通过文件名下载文件
     */
    @RequestMapping("/testDownload")
    @ResponseBody
    public String testDownload() {
        AliyunOSSUtil aliyunOSSUtil = new AliyunOSSUtil();
        aliyunOSSUtil.downloadFile(
                "static/images/2019-04-20/6a7dc75bfdd04150ba9fbbaf3aed50ea-timg.jpg","D:\\file\\oss\\");
        return "success";
    }

    /**
     * 列出某个文件夹下的所有文件
     */
    @RequestMapping("/testListFile")
    @ResponseBody
    public String testListFile() {
        AliyunOSSUtil aliyunOSSUtil = new AliyunOSSUtil();
        aliyunOSSUtil.listFile();
        return "success";
    }

    /**
     * 文件上传
     */
    @RequestMapping(value = "/uploadFile")
    public String uploadPicture(@RequestParam("file") MultipartFile file,
                                Model model,
                                HttpServletRequest request) {

        logger.info("文件上传");
        Long fileSize = (file.getSize()/1024*100)/100;
        String filename = file.getOriginalFilename();
        try {

            if (file != null) {
                if (!"".equals(filename.trim())) {
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);
                    // 上传到OSS
                    String uploadUrl = aliyunOSSUtil.upLoad(newFile,request,fileSize);
                    model.addAttribute("url",uploadUrl);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "success";
    }

    /**
     * 获取进度条
     */
    @GetMapping(value = "/getProgressBar")
    @ResponseBody
    public UploadVO getProgressBar(@RequestParam("uploadNo") String uploadNo) {

        UploadVO uploadVO ;
        for (;;) {
            uploadVO = CacheUpload.getProgressBar(uploadNo);
            if (Objects.nonNull(uploadVO)) {
                logger.info("getProgressBar.resp uploadVO={}", uploadVO.toString());
                if (uploadVO.getProgressBar().compareTo(new BigDecimal(100.00)) == 0) {
                    break;
                }
            }
        }
        CacheUpload.removeProgressBar(uploadNo);
        return uploadVO;
    }

    @RequestMapping("/downLoad")
    public void download( HttpServletResponse response) {
        aliyunOSSUtil.downFileOSS("static/images/2019-04-20/6a7dc75bfdd04150ba9fbbaf3aed50ea-timg.jpg",response);
    }

}
